package com.xidian.zhiancloud.service.ledgerManager.targerAndSystem;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xidian.zhiancloud.dao.SecurityMeetingLedgerMapper;
import com.xidian.zhiancloud.dao.syset.SysUserMapper;
import com.xidian.zhiancloud.dao.syset.usermanage.OrganizationMapper;
import com.xidian.zhiancloud.enums.GeneralResponseEnums;
import com.xidian.zhiancloud.exception.CustomizeException;
import com.xidian.zhiancloud.model.*;
import com.xidian.zhiancloud.model.syset.Organization;
import com.xidian.zhiancloud.model.syset.SysUser;
import com.xidian.zhiancloud.qo.ledgerManagerment.targetAndSystem.SecurityMeetingLedgerQO;
import com.xidian.zhiancloud.util.zyf.StringUtils;
import com.xidian.zhiancloud.vo.ledgerManagerment.targetAndSystem.SecurityMeetingLedgerVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Author: Summerless
 * @Date: 2020/11/8 23:24
 */
@Service
public class SecurityMeetingLedgerService extends ServiceImpl<SecurityMeetingLedgerMapper, SecurityMeetingLedger> implements IService<SecurityMeetingLedger> {
    @Autowired
    private SecurityMeetingLedgerMapper securityMeetingLedgerMapper;
    @Autowired
    private OrganizationMapper organizationMapper;
    @Autowired
    private SysUserMapper sysUserMapper;

    public IPage<SecurityMeetingLedgerVO> selectByQO(SecurityMeetingLedgerQO securityMeetingLedgerQO) {
        Page<SecurityMeetingLedgerVO> page = new Page<>(securityMeetingLedgerQO.getCurrent(), securityMeetingLedgerQO.getSize());
        OrderItem orderItem = new OrderItem();
        orderItem.setColumn("create_time").setAsc(false);
        page.getOrders().add(orderItem);
        IPage<SecurityMeetingLedgerVO> ans = securityMeetingLedgerMapper.selectByQO(page, securityMeetingLedgerQO);
        return ans.convert(s ->{
            String[] split_org = s.getParticipateOrganizationIds().split(",");
            List<String> orgnames = Arrays.stream(split_org).map(t -> {
                Long tmp_id = Long.parseLong(t);
                Organization tmp = organizationMapper.selectById(tmp_id);
                return tmp == null ? "该部门已被删除" : tmp.getName();
            }).collect(Collectors.toList());
            s.setParticipateOrganizationNames(StringUtils.concatStrings(orgnames));
            String[] split_peo = s.getPeoIds().split(",");
            List<String> peoNames = Arrays.stream(split_peo).map(t -> {
                SysUser tmp = sysUserMapper.selectById(t);
                return tmp == null ? "该用户已被删除" : tmp.getName();
            }).collect(Collectors.toList());
            s.setPeoNames(StringUtils.concatStrings(peoNames));
            return s;
        });
    }

    public SecurityMeetingLedgerVO getVOById(Long id) {
        SecurityMeetingLedger ledger = securityMeetingLedgerMapper.selectById(id);
        if (ledger == null) {
            throw new CustomizeException(GeneralResponseEnums.GET_FAILED);
        }
        SecurityMeetingLedgerVO vo = new SecurityMeetingLedgerVO();
        BeanUtils.copyProperties(ledger, vo);
        String[] split_org = ledger.getParticipateOrganizationIds().split(",");
        List<String> orgnames = Arrays.stream(split_org).map(t -> {
            Organization tmp = organizationMapper.selectById(t);
            return tmp == null ? "该部门已被删除" : tmp.getName();
        }).collect(Collectors.toList());
        vo.setParticipateOrganizationNames(StringUtils.concatStrings(orgnames));

        String[] split_peo = vo.getPeoIds().split(",");
        List<String> peoNames = Arrays.stream(split_peo).map(t -> {
            SysUser tmp = sysUserMapper.selectById(t);
            return tmp == null ? "该用户已被删除" : tmp.getName();
        }).collect(Collectors.toList());
        vo.setPeoNames(StringUtils.concatStrings(peoNames));

        SysUser moderator = sysUserMapper.selectById(vo.getModeratorId());
        vo.setModeratorName(moderator == null ? "该用户已被删除" : moderator.getName());
        SysUser recorder = sysUserMapper.selectById(vo.getRecorderId());
        vo.setRecorderName(recorder == null ? "该用户已被删除" : recorder.getName());
        SysUser creator = sysUserMapper.selectById(vo.getCreatorId());
        vo.setCreatorName(creator == null ? "该用户已被删除" : creator.getName());
        return vo;
    }
}
